package astri.pdcontrol.commclient;

import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * This class holds all the event handlers of AstCommClient.
 * @author Shing, Echo @ASTRI
 *
 */
public interface AstCommClientListener {
	
	/**
	 * Receive Message Event
	 * @param sock		server socket
	 * @param msg	Message from server
	 * @return	(for future use)
	 */
	int onReceive(Socket sock, AstCommMessage msg);
	
	/**
	 * Disconnect Event
	 * @param sock	server socket
	 * @return	(for future use)
	 */
    int onDisconnect(Socket sock);
    
    /* @See onClientTimeout
     * !!! return value handle is NOT implemented !!!
    return 0 to reconnect to server,
    return 2 to wait for connection,
    return -1 to stop client thread
    */
    //int onClientTimeout(AstSocket sock);
	
    /* New implemented in PDControl 
     * for getting error when AstCommClientSend*/
    //void onSendError();

	/*
	 * return 0 to continue discovery,
	 * return 1 to connect to the server,
	 * return 2 to stop dicovery mode and wait for connection
	 */
	//int onInit(AstCommDiscovery init);
	
    /**
     * A new Server Discovered and put into list
     * @return	(for future use)
     */
    int onDiscovery(InetSocketAddress sockAddr);
    
    /**
     * When connected to a server
     * @param sock	server socket
     * @return	(for future use)
     */
    int onConnect(Socket sock);
    
    /**
     * When connect with non-blocking
     * @param connected		is connected?		
     * @param sockAddr		connecting Address
     * @return	(for future use)
     */
    int onConnectNonBlock(boolean connected, InetSocketAddress sockAddr);
     
    /*
    return 0 to continue reconnect,
    return 2 to wait for connection,
    return -1 to stop client thread
    !!! Careful, race condition may occur if you call AstCommClientStop()
        (even async) and return 2 with this handler 
 	   AstCommClientStop()'s state change will be over-written !!!
  */
   // int onServerTimeout(AstSocket sock);

    /*
    return 0 to enter connected mode,
    return 1 to forward the received message to OnReceive
    return 2 to ignore the message and maintain current state
    return -1 to stop client thread
    !!! Careful, race condition may occur if you call AstCommClientStop()
        (even async) and return 2 with this handler 
 	   AstCommClientStop()'s state change will be over-written !!!
    */
   // int onReconnectError(AstSocket sock);
    
    /*
    return -1 to stop client thread  // always stop thread, 16/8
    */
  //  int onError(int err, Object info);

    /*
    return 0 to enter connected mode,
    return 1 to forward the received message to OnReceive
    return 2 to ignore the message and maintain current state
    return -1 to stop client thread
    !!! Careful, race condition may occur if you call AstCommClientStop()
        (even async) and return 2 with this handler 
 	   AstCommClientStop()'s state change will be over-written !!!
    */
  //  int onLogin(AstCommMessage command);

    /*
    return 0 to enter connected mode,
    return -1 to stop client thread
    !!! Careful, race condition may occur if you call AstCommClientStop()
        (even async) and return 2 with this handler 
 	   AstCommClientStop()'s state change will be over-written !!!
  */
  //  int onRelogin();

}
